package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 *
 * @author Yanxinhao on 2024/12/25
 */
@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {
    @Autowired
    private CartService cartService;
    @Operation(summary = "添加商品到购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@Parameter(description = "商品sku规格的id")
                                @PathVariable Long skuId,
                                @Parameter(description = "数量")
                                @PathVariable Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return success();
    }

    @Operation(summary = "查询购物车列表")
    @GetMapping("/cartList")
    public AjaxResult cartList(){
        return success(cartService.getCartList());
    }

    @Operation(summary = "删除购物车中的商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@Parameter(name = "商品skuId")
                                 @PathVariable Long skuId){
        cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary = "修改购物车中商品的选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@Parameter(name = "商品skuId")
                                @PathVariable Long skuId,
                                @Parameter(name = "是否选中 1:选中 0:未选中")
                                @PathVariable Integer isChecked){
        cartService.checkCart(skuId,isChecked);
        return success();
    }
    @Operation(summary = "全选/全不选")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@Parameter(name = "是否选中 1:选中 0:未选中")
                                       @PathVariable Integer isChecked){
        cartService.allCheckCart(isChecked);
        return success();
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }

    @InnerAuth //这个接口负责供给给其他微服务模块远程调用，并非购物车模块下自己调用的接口
    @Operation(summary = "获取用户购物车选中商品列表")
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList (){
        return R.ok(cartService.getCartCheckedList());
    }

    @InnerAuth
    @Operation(summary = "更新用户购物车列表的价格")
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable Long userId){
        return R.ok(cartService.updateCartPrice(userId));
    }

    @InnerAuth
    @Operation(summary = "付款成功后删除购物车选中商品")
    @GetMapping("/deleteCartCheckedList/{userId}")
    public R<Boolean> deleteCartCheckedList(@PathVariable Long userId,
     @RequestHeader(SecurityConstants.FROM_SOURCE) String source){
        return R.ok(cartService.deleteCartCheckedList(userId));
    }
}